<template>
  <el-dialog :visible.sync="isShow" v-if="isShow" title="短信配置" width="50%" :before-close="handleClose" top="150px" :close-on-click-modal="false">
    <div class="border-box">
      <el-form ref="emailForm" :model="formParams" :rules="ruleForm" label-position="right" label-width="0">
        <div class="flex item-box" v-for="(item,index) in itemData" :key="index">
          <div class="title width25">
            {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
          </div>
          <div class="content width80">
            <el-form-item class="item-clear" v-show="item.key=='account'" label="" prop="account">
              <el-input class="width50" type="text" v-model="formParams.account" clearable placeholder="请输入账号"></el-input>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='password'" label="" prop="password">
              <el-input class="width50" type="text" v-model="formParams.password" clearable placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='cate'" label="" >
            <el-select class="width80" v-model="formParams.cate" placeholder="请选择短信运营商" style="width:200px">
              <el-option label="nodesms运营商" :value="1"></el-option>
              <el-option label="nbfmg运营商" :value="2"></el-option>
            </el-select>
          </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='isClose'" label="" style="height:50px">
              <el-radio-group v-model="formParams.isClose">
                <el-radio-button :label="0">开启</el-radio-button>
                <el-radio-button :label="1">关闭</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('emailForm')">确 认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addMessage,
  editMessage
} from "@/api/config";

export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
  },
  data () {
    return {
      isShow: false,
      itemData:[
        { label: '账户', key: 'account' ,pass:true},
        { label: '密码', key: 'password' ,pass:true},
        { label: '运营商', key: 'cate' },
        { label: '开启关闭', key: 'isClose' },
      ],
      formParams: {
        account: '', //账号
        password: '', //密码
        isClose: 0, //是否关闭 1是关闭
        cate:1 // 短信运营商
      },
      ruleForm: {
        account: [{ required: true, trigger: 'blur', message: '请输入账户', }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码', }],
      },
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
      if (that.data) {
        that.formParams = JSON.parse(JSON.stringify(that.data))
      } else {
        that.formParams = {
          account: '', //账号
          password: '', //密码
          isClose: 0, //是否关闭 1是关闭
          cate:1
        }
      }
    },
  },
  mounted () { },
  methods: {
    // 确认
    handleConfirm (formvalue) {
      let that = this,
      params = JSON.parse(JSON.stringify(that.formParams));
      that.$refs[formvalue].validate((valid) => {
        if (valid) {
          if (!params.id) {
            addMessage(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          } else {
            editMessage(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
</style>
